package cn.edu.stu.syllabus.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.datas.OaCompany;
import cn.edu.stu.syllabus.utils.ExtraNameJoiner;
import cn.edu.stu.syllabus.widgets.TextInputSpinner;

/**
 * Created by liyujie on 2017/3/17.
 */

public class SearchOaDialog extends DialogFragment {

    public static final String DIALOG_SEARCH_OA = SearchOaDialog.class.getCanonicalName();

    private static final String EXTRA_LAST_POSITION = ExtraNameJoiner.join(
            SearchOaDialog.class, "lastPosition");

    private static final String EXTRA_LAST_KEY_WORD = ExtraNameJoiner.join(
            SearchOaDialog.class, "lastKeyWord");

    @BindView(R.id.companyTextInputSpinner)
    TextInputSpinner mCompanyTextInputSpinner;
    @BindView(R.id.keywordEditText)
    EditText mKeywordEditText;

    private int mLastPosition;

    private String mLastKeyWord;

    public interface OnSearchButtonClickListener {
        void onSearchButtonClick(String keyword, int position);
    }

    OnSearchButtonClickListener mOnSearchButtonClick;

    public static SearchOaDialog newInstance(String lastKeyWord, int lastPosition) {
        SearchOaDialog searchOaDialog = new SearchOaDialog();
        Bundle args = new Bundle();
        args.putString(EXTRA_LAST_KEY_WORD, lastKeyWord);
        args.putInt(EXTRA_LAST_POSITION, lastPosition);
        searchOaDialog.setArguments(args);
        return searchOaDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        mLastPosition = args.getInt(EXTRA_LAST_POSITION);
        mLastKeyWord = args.getString(EXTRA_LAST_KEY_WORD);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity())
                .inflate(R.layout.dialog_search_oa, null, false);
        ButterKnife.bind(this, view);

        final String[] oaNameItems = OaCompany.getOaCompaniesNameStringArray();
        mCompanyTextInputSpinner.setItems(oaNameItems);

        mKeywordEditText.setText(mLastKeyWord);
        mCompanyTextInputSpinner.setSelection(mLastPosition);

        return new AlertDialog.Builder(getActivity())
                .setTitle("搜索")
                .setView(view)
                .setPositiveButton("搜索", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (getActivity() != null && mOnSearchButtonClick != null) {
                            mOnSearchButtonClick.onSearchButtonClick(
                                    mKeywordEditText.getText().toString(),
                                    mCompanyTextInputSpinner.getSelectedItemPosition());
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .create();
    }

    public OnSearchButtonClickListener getOnSearchButtonClick() {
        return mOnSearchButtonClick;
    }

    public void setOnSearchButtonClick(OnSearchButtonClickListener onSearchButtonClick) {
        mOnSearchButtonClick = onSearchButtonClick;
    }
}
